layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    var ToolExpress = {
        tableId: "toolExpressTable"
    };
    
    /**
     * 初始化表格的列
     */
    ToolExpress.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'customerName', sort: true, title: '客户'},
            {field: 'wellen', sort: true, title: '波次'},
            {field: 'v1', sort: true, title: '出库单号'},
            {field: 'orderNumber', sort: true, title: '快递单号'},
            {field: 'goodsNumber', sort: true, title: '商品组合'},
            {field: 'num', sort: true, title: '组合数量'},
            {field: 'v2', sort: true, title: '重量'},
            {field: 'v3', sort: true, title: '快递公司'},
            {field: 'v4', sort: true, title: '省份'},
            {field: 'v5', sort: true, title: '城市'},
            {field: 'v6', sort: true, title: '区县'},
            {field: 'v7', sort: true, title: '街道'},
            {field: 'v8', sort: true, title: '地址'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolExpress.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ToolExpress.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    ToolExpress.exportExcel = function () {
        var checkRows = table.checkStatus(ToolExpress.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
        	console.log()
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolExpress.tableId,
        url: Feng.ctxPath + '/toolExpress/wellenList?waveId='+Feng.getUrlParam("waveId")+"&wellen="+Feng.getUrlParam("wellen"),
        page: true,
        limit: 40,
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarfrom',
		limits:[40,100,150,500,1000],
        cellMinWidth: 100,
        cols: ToolExpress.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolExpress.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolExpress.exportExcel();
    });
		
	//批量复制    
    $('#copyOrder').click(function () {
    	var checkStatus = table.checkStatus('toolExpressTable');
        data = checkStatus.data;
        if (data.length>0) {
			var orders = "";
            for (var i = 0; i < data.length; i++) {
				orders += data[i].orderNumber;
				orders += "\n";
			}
			// 获取要复制的文本
			const textToCopy = orders;
			
			// 创建一个临时的textarea元素，并将文本内容设置为要复制的文本
			const tempTextarea = document.createElement("textarea");
			tempTextarea.value = textToCopy;
			
			// 将textarea添加到DOM中
			document.body.appendChild(tempTextarea);
			
			// 选中textarea中的文本
			tempTextarea.select();
			
			// 执行复制命令
			document.execCommand("copy");
			
			// 删除临时创建的textarea元素
			document.body.removeChild(tempTextarea);
			
        	Feng.success("复制成功!");
		} else {
			layer.alert('请至少选中一条数据');
		}
    });

    $('#btnDelete').click(function () {
    	var checkStatus = table.checkStatus('toolExpressTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/toolExpress/wellenbtnDelete?ids="+ids, function (data) {
	                if(data.success){
	                	Feng.success("删除成功!");
	                	table.reload(ToolExpress.tableId);
	                } else {
	                    Feng.error("删除失败!" + data.message + "!");
	                }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });
});
